package cn.edu.nju.oraldefense.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.edu.nju.oraldefense.dao.CommentDao;
import cn.edu.nju.oraldefense.model.Comment;
import cn.edu.nju.oraldefense.model.Paper;
import cn.edu.nju.oraldefense.model.Teacher;

@Component("commentDao")
public class CommentDaoImpl implements CommentDao {

	HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(Comment instance) {
		hibernateTemplate.save(instance);
	}

	public Comment findById(int id) {
		Comment instance = (Comment) getHibernateTemplate().get(
				"cn.edu.nju.oraldefense.model.Comment", id);
		return instance;
	}

	public List findByProperty(String propertyName, Object value) {
		String queryString = "from Comment as model where model."
				+ propertyName + "=? order by time desc";
		return hibernateTemplate.find(queryString, value);
	}

	public List findByPaper(Paper paper) {
		List list = findByProperty("paper", paper);
		/*
		 * Comment comment = null; if (list != null && list.size() > 0) comment
		 * = (Comment) list.get(0);
		 */
		return list;
	}

	public List findByTeacherAndMonth(Teacher teacher, String yearmonth,String nextyearmonth) {
		String queryString = "from Comment as model where model.time>='" + yearmonth
				+ "' and model.time<'" + nextyearmonth + "' and teacher=?";
		System.out.println(queryString);
		return hibernateTemplate.find(queryString, teacher);

	}

	public void update(Comment instance) {
		hibernateTemplate.update(instance);
	}

	public void delete(Comment instance) {
		hibernateTemplate.delete(instance);
	}
}
